
capture log close
log using $log/AED_analysis_12_graphs, text replace

/* ------------------------------------------------------------------------------------ */
/* --- Estimation results Figure B.1 -------------------------------------------------- */
/* ------------------------------------------------------------------------------------ */ 


clear
clear matrix
clear mata
set maxvar 10000
set matsize 2000
set more off

set trace off
   
#d;
global xvars " vac_rate aloq   
				share_agriculture_avg share_industry_avg share_service_avg wanderungen_avg bip_avg 
				iza_alter_kat1 iza_alter_kat2 iza_alter_kat3 iza_alter_kat4 iza_sex iza_famstd
				i.iza_kind i.iza_abschluss_kat i.iza_ausb_kat
				iza_last_daily_income i.iza_beruf_kat
				iza_ende_last iza_vztz_last iza_zeit_la
				iza_sv_tminus1 iza_sv_tminus2 iza_sv_tminus3
				iza_lnwage_tminus1 iza_lnwage_tminus2 iza_lnwage_tminus3 
				i.year i.entry_m";
				#d cr
				

		use $data/data_est_border, clear
		
		*Restrict to same inflows as in survey
		keep if eintritt<=200805 & eintritt>=200706
	
	
		#d;
		egen month_sv1_24 = rsum(sv_tplus_1 sv_tplus_2 sv_tplus_3 sv_tplus_4 sv_tplus_5 sv_tplus_6 sv_tplus_7 sv_tplus_8 sv_tplus_9 sv_tplus_10 
		sv_tplus_11 sv_tplus_12 sv_tplus_13 sv_tplus_14 sv_tplus_15 sv_tplus_16 sv_tplus_17 sv_tplus_18 sv_tplus_19 sv_tplus_20 sv_tplus_21 sv_tplus_22 sv_tplus_23 sv_tplus_24)
		;
	
		egen earnings1_24 = rsum(daily_income_tplus_1 daily_income_tplus_2 daily_income_tplus_3 daily_income_tplus_4 daily_income_tplus_5
		daily_income_tplus_6 daily_income_tplus_7 daily_income_tplus_8 daily_income_tplus_9 daily_income_tplus_10 daily_income_tplus_11
		daily_income_tplus_12 daily_income_tplus_13 daily_income_tplus_14 daily_income_tplus_15 daily_income_tplus_16 
		daily_income_tplus_17 daily_income_tplus_18 daily_income_tplus_19 daily_income_tplus_20
		daily_income_tplus_21 daily_income_tplus_22 daily_income_tplus_23 daily_income_tplus_24);
		replace earnings1_24 = earnings1_24*30;
		#d cr
	
	/* Regression for treatment intensity residuals */
   
   reg log_intensity_alt $xvars `opt'
	predict pred_int,xb
	gen res_int=log_intensity_alt - pred_int
	
	qui sum res_int,d
	drop if res_int>r(p99)|res_int<r(p1)
	
		
	forvalues x=1/2 {
	
	use `help', clear
	
		if `x'==1 {
			local var "month_sv1_24"
			local name "total no. of months employed"
		}
		if `x'==2 {
			local var "earnings1_24"
			local name "total labor earnings"
		}
	
	bys iza_id: gen ind_sample=1 if _n==1 	
	
	/* Regression for outcome residuals */
    
	reg `var' $xvars `opt'
	predict pred_`var',xb
	gen res_`var'=`var'-pred_`var'
	count if e(sample)==1 & ind_sample==1
	local obs = r(N)
	
	
	/* Histogram */
	#d;
	twoway (hist res_int, frac yaxis(1))
	(lpolyci res_`var' res_int, fintensity(inten20) level(90) yaxis(2)
	ytitle("Residual variation in `name'", axis(2))
	yline(0, lpattern(dash) axis(2))),
	scheme(s2mono) graphregion(color(white)) legend(off) 
	note("No. of observations = `obs'")
	xtitle("Residual variation in log treatment intensity");
	#d cr
	*Export graph for publication mode
	graph save $log/dist_`var'.gph, replace asis
	}
	
log close
